<?php
require_once('inc/init.php');
require_once getCacheFilePath('forums.php');
require_once getCacheFilePath('links.php');
require_once getCacheFilePath('ads.php');
require_once getCacheFilePath('announces.php');

//获取广告
$adcode=getAd('index');
$fid=intval($_GET['fid']);
if(!empty($fid)){
	$headkeywords=(empty($cache_forums[$fid]['metakeywords'])?"":$cache_forums[$fid]['metakeywords'].",");	
	$headdesc=(empty($cache_forums[$fid]['forumname'])?"":strip_tags($cache_forums[$fid]['forumname']).",");
}
require_once('header.php');

$online_row=$db->row_query("Select k,Count(0) As ct From(Select case when userid=0 then 'eq0' else 'neq0' end as k From `{$db->pre}online`) A Group By k");
$online_guessnum=0;
$online_usernum=0;
foreach($online_row as $tmp){
	if($tmp['k']=='eq0'){
		$online_guessnum=intval($tmp['ct']);
	}elseif($tmp['k']=='neq0'){
		$online_usernum=intval($tmp['ct']);
	}
}
$online_allnum=$online_usernum+$online_guessnum;

//更新今日/昨日统计数据
$cache1=$db->row_select_one("cache1");
if(trim($cache1['todaydate'])!=trim(getDateStr(time(),'dateonly',false))){
	$cache1['yesterdaynum']=$cache1['todaynum'];
	$cache1['todaydate']=getDateStr(time(),'dateonly',false);
	if($cache1['todaynum']>$cache1['mostnum']){
		$cache1['mostnum']=$cache1['todaynum'];
	}
	$cache1['todaynum']=0;
	$db->row_update("cache1",$cache1,"");
	$forum_new['todaynum']=0;
	$db->row_update("forums",$forum_new,"");
	$user_new['todayuploaded']=0;
	$db->row_update("users", $user_new, "");
}
if($online_allnum>$cache1['mostonlinenum']){
	$cache1_new['mostonlinenum']=$cache1['mostonlinenum']=$online_allnum;
	$cache1_new['mostonlinetime']=$cache1['mostonlinetime']=$_SYS['time'];
	$db->row_update("cache1", $cache1_new);
}
$cache1['mostonlinetime']=getDateStr($cache1['mostonlinetime']);
$cache1['lastuser']=htmlFilter($cache1['lastuser']);

$forums_rows=$db->row_select("forums","",0,"id,todaynum,topicsnum,postsnum,lastuser,lastpost,lastposttime,lasttid,admins,picurl,forumpass,forumusers,forumgroups");
foreach($forums_rows as $forum_row){
	$cache_forums[$forum_row['id']]['todaynum']=$forum_row['todaynum'];
	$cache_forums[$forum_row['id']]['topicsnum']=$forum_row['topicsnum'];
	$cache_forums[$forum_row['id']]['postsnum']=$forum_row['postsnum'];
	$cache_forums[$forum_row['id']]['lastuser']=$forum_row['lastuser'];
	$cache_forums[$forum_row['id']]['lastpost']=htmlFilter(cutStr($forum_row['lastpost'],30));
	$cache_forums[$forum_row['id']]['lastposttime']=empty($forum_row['lastposttime'])?"":getDateStr($forum_row['lastposttime']);
	$cache_forums[$forum_row['id']]['lasttid']=$forum_row['lasttid'];
	$cache_forums[$forum_row['id']]['adminsstr']=getAdminStr($forum_row['admins']);
	$cache_forums[$forum_row['id']]['forumicon']=empty($forum_row['picurl'])?($_SYS['TP'].( (!empty($forum_row['forumpass']) || !empty($forum_row['forumusers']) || !empty($forum_row['forumgroups']) )? "/images/ico_haslock.png"  : ($forum_row['todaynum']>0?"/images/ico_hasnew.png":"/images/ico_hasnonew.png"))):$forum_row['picurl'];
}

//获取公告
$announce_index="";
$announces_arr= array();
if(is_array($cache_announces_tree['all'])){
	$announces_arr=array_merge($announces_arr, $cache_announces_tree['all']);
}
if(is_array($cache_announces_tree['index'])){
	$announces_arr=array_merge($announces_arr, $cache_announces_tree['index']);
}

foreach($announces_arr as $ancid){
	$announce=$cache_announces[$ancid];
	if($announce['starttime']>$_SYS['time'] || ($announce['stoptime']<$_SYS['time'] && $announce['stoptime']>0)){
		continue;
	}
	$announce['posttime']=getDateStr($announce['posttime']);
	$announce['title']=htmlFilter($announce['title']);
	$anclink = numFilter($announce['type'])==1? $announce['url']:"announce.php?id=$announce[id]";
	$announce_index.="<span><a href=\"{$anclink}\" target=\"_blank\" style=\"color:{$announce['color']}\">{$announce['title']}</a></span>";
}

//用户资料
if(isLogin()){
	$mygroup=$cache_groups[$lg['groupid']];
	$mygroupname = $mygroup['groupname'];
	$mygroupstar=getStar($mygroup['starnum']);
}

//在线列表
if(numFilter($cache_settings['indexmostonline'])>0){
	$index_onlines = $db->row_query("Select * From (Select (case When userid=0 then 0 else 1 end) As user_mark,{$db->pre}online.* From {$db->pre}online) {$db->pre}online Order By user_mark desc,activetime desc limit {$cache_settings['indexmostonline']}");
	foreach($index_onlines as $key=>$index_online){
		$index_online['activetime']=getDateStr($index_online['activetime']);
		$online_alt = ($lg['groupid']<GROUP_FORUMADMIN)? "最后活动:{$index_online['activetime']}" : "最后活动:{$index_online['activetime']}&#13;IP地址:{$index_online['ip']}";
		$index_online['name']=$index_online['userid']==0?"<span title=\"{$online_alt}\">游客</span>":"<a href='".genUrl("user.php?uid={$index_online['userid']}")."' target='_blank' title='{$online_alt}'>{$index_online['username']}</a>";
		$index_online['onlineicon']=0;
		if($index_online['groupid']>200){
			$index_online['onlineicon']=$index_online['groupid'];
		}elseif($index_online['groupid']>100){
			$index_online['onlineicon']=0;
		}else{
			$index_online['onlineicon']=1;
		}
		$index_onlines[$key]=$index_online;
	}
}

//是否有新短信
$rows_msg=$db->row_select_one("msgs", "touserid={$lg['userid']} and hasread=0","count(0) as c");
if($rows_msg['c']>0){
	$msg_link="<img src=\"images/ico_newmsg.gif\" border=\"0\" align=\"absmiddle\" /> <a href=\"member.php?action=msg\">{$rows_msg['c']}条新短信</a>";
}else{
	$msg_link="<a href=\"member.php?action=msg\">站内短信</a>";
}

require_once getTemplatePath('index.htm');
footer();
?>